package com.fr.lintcode;

import java.util.Stack;

import org.junit.Test;

/**
*作者：furong
*日期：2017年3月2日
*时间：下午5:55:37
*/
public class Q421 {
    /**
     * @param path the original path
     * @return the simplified path
     */
    public String simplifyPath(String path) {
        if (path == null || path.length() <= 0) {
            return "/";
        }
        String ss[] = path.split("/");
        Stack<String> stack = new Stack<>();
        for (String s : ss) {
            if (s.length() == 0 || ".".equals(s)) {
                continue;
            } else if ("..".equals(s)) {
                if (!stack.isEmpty()) {
                    stack.pop();
                }
            } else {
                stack.push(s);
            }
        }
        String rs = "";
        while (!stack.isEmpty()) {
            rs = "/" + stack.pop() + rs;
        }
        if (rs.startsWith("/")) {
            return rs;
        } else {
            return "/";
        }
    }

    @Test
    public void testA() {
        String path = "/a/./b/../../c/";
        System.out.println(simplifyPath(path));
    }
}
